package com.cskaoyan.mall.commons.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 创建日期: 2022/04/25 09:43
 *
 * @author ChengWenqi
 */

@Slf4j
public class UserInfoUtil {

    /**
     * 验证用户是否登录，获取用户id
     * 创建日期：2022/04/22 23:07
     * @param  servletRequest
     * @return java.lang.Long 用户id
     * @author ChengWenqi
     */
    public static Long checkLoginAndGetUserId(HttpServletRequest servletRequest) {
        // 验证是否已登录：从 servletRequest 里取出请求头 user_info，获取用户id
        String userInfo = servletRequest.getHeader("user_info");
        Map<String, String> userInfoMap = (Map<String, String>) JSON.parse(userInfo);
        String id = userInfoMap.get("id");
        log.info(id);
        return Long.parseLong(id);
    }
}
